<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/welcome'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>留言箱管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--修改对话框-->
    <!-- <el-row :gutter="12">
      <el-col :span="6">
        <el-button type="success"
                   icon="el-icon-plus"
                   @click="addDialog">添加</el-button>
      </el-col>
    </el-row> -->
    <!--表格列表-->
    <el-table :data="tableData">
      <el-table-column prop="messageId"
                       label="留言ID"></el-table-column>
      <el-table-column prop="messageTitle"
                       label="主题"></el-table-column>
      <el-table-column prop="messageContent"
                       label="留言内容"></el-table-column>
      <el-table-column prop="adminReple"
                       label="管理员回复"></el-table-column>
      <el-table-column prop="createTime"
      min-width="100px"
                       label="创建时间"></el-table-column>
      <el-table-column prop="username"
                       label="用户名"></el-table-column>
      <el-table-column prop="name"
                       label="姓名"></el-table-column>
      <el-table-column align="center"
      fixed="right" min-width="150px"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="success"
          v-if="scope.row.adminReple==null||scope.row.adminReple==''"
                     @click="getOne(scope.row)">回复</el-button>
          <el-button type="info"
            v-else disabled
                     @click="getOne(scope.row)">已回复</el-button>
          <el-button type="danger"
                     @click="deleteItem(scope.row)">删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="pager.current"
                   :page-size="pager.size"
                   :page-sizes="[5, 10, 15, 20]"
                   :total="pager.total"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!--修改对话框-->
    <el-dialog :visible.sync="editFlag"
               :close-on-click-modal="false"
               title="回复留言">
      <el-form>
        <el-form-item label="留言ID">
          <el-input v-model="form.messageId" disabled
                    ></el-input>
        </el-form-item>
        <el-form-item label="主题">
          <el-input v-model="form.messageTitle" disabled
                    placeholder="请输入主题"></el-input>
        </el-form-item>
        <el-form-item label="留言内容">
          <el-input v-model="form.messageContent" disabled
                    placeholder="请输入留言内容"></el-input>
        </el-form-item>
        <el-form-item label="管理员回复">
          <el-input v-model="form.adminReple"
                    placeholder="请输入管理员回复"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="editFlag = false">取 消</el-button>
        <el-button type="primary"
                   @click="update">确 定</el-button>
      </div>
    </el-dialog>
    <!--添加对话框-->
    <el-dialog :visible.sync="addFlag"
               :close-on-click-modal="false"
               title="添加咨询公告信息">
      <el-form>
        <el-form-item label="主题">
          <el-input v-model="form.messageTitle"
                    placeholder="请输入主题"></el-input>
        </el-form-item>
        <el-form-item label="留言内容">
          <el-input v-model="form.messageContent"
                    placeholder="请输入留言内容"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="form.createTime"
                    placeholder="请输入创建时间"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"
                    placeholder="请输入用户名"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="addFlag = false">取 消</el-button>
        <el-button type="primary"
                   @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {msgBoxList, deleteMsgBox, getOneMsgBox, saveMsgBox, updateMsgBox} from "../../api/api";

export default {

  data () {
    return {
      addFlag: false, // 显示添加对话框的标记
      editFlag: false,
      tableData: [],
      pager: {
        current: 1,
        size: 5,
        total: 0
      },

      listMsgbox: [],
      form: {
        messageId: 0,
        messageTitle: '',
        messageContent: '',
        adminReple: '',
        createTime: '',
        username: ''
      },
      queryForm:{
        pageSize: 1,
        pageNum: 5,
      },

    }
  },

  methods: {
    msgboxLists () {
      this.queryForm.pageSize = this.pager.size;
      this.queryForm.pageNum = this.pager.current;
      msgBoxList(this.queryForm)
        .then((response) => {
          if (response.code === 200) {
            console.log(response);
            this.$message.success(response.msg);
            this.tableData = response.rows;
            this.pager.total = response.total;
          }
          else {
            this.$message.error(response.msg)
          }
        });
    },
    reset(){
      this.form={
        userId: 0,
        username: '',
        name: '',
        age: '',
        sex: '',
        idCard: '',
        phone: '',
        address: '',
      }
    },
    addDialog(){
      this.reset()
      this.addFlag = !this.addFlag;
    },
    getOne (row) {
      this.editFlag = !this.editFlag;
      getOneMsgBox(row.messageId)
        .then((response) => {
          if (response.code === 200) {
            this.form = response.data;
          } else {
            this.$message.error("接口出错")
          }
        });
    },
    deleteItem(row){

       this.$modal.confirm('是否确认删除所有活动管理编号为"' + row.messageId + '"的留言？')
       .then(function() {
        return deleteMsgBox(row.messageId);
      }).then(() => {
        this.msgboxLists();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    update(){
      //前端校验,加钱才做
      //发送请求到后端
      updateMsgBox(this.form)
        .then((response) => {
          if (response.code === 200){
            this.$message.success(response.msg);
            this.msgboxLists(this.pager);
            this.editFlag = false
          }
          else{
            this.$message.error(response.msg);
          }
        });
    },
    save(){
      saveMsgBox(this.annos)
        .then((response) => {
          if (response.code === 200){
            this.$message.success(response.msg);
            this.msgboxLists(this.pager);
            this.addFlag = false
          }
          else{
            this.$message.error(response.msg);
          }
        });
    },

    handleCurrentChange (val) {
      this.pager.current = val;
      this.msgboxLists(this.pager);
    },
    handleSizeChange (val) {
      this.pager.size = val;
      console.log(this.pager)
      this.msgboxLists(this.pager);
    },

  },
  created () {
    this.msgboxLists(this.pager);
  }
}
</script>

<style>
.el-breadcrumb {
  padding-bottom: 12px;
}
.el-pagination {
  padding-top: 12px;
}
</style>
